import { defineStore } from "pinia";
import { Names } from "./store-name";
import { queryMailInfo, updateMailInfo } from '../api/config';
import { useRequest } from "alova";
import type { TEmailInfo, TResponse } from "@/typing/base";
import type { TEmailInfoRes } from "@/typing/config";


const useConfigStore = defineStore(Names.CONFIG, {
  state() {
      return {
        mailInfo: <TEmailInfo>{},
        isLoading: false
      }
  },

  getters: {
    getLoading(state): boolean {
      return state.isLoading
    }
  },
  actions: {
    queryMailInfo() {
      // this.isLoading = true
      this.$patch(state => { state.isLoading = true })
      const { onSuccess } = useRequest(queryMailInfo())
      onSuccess(({ data }: { data: TEmailInfoRes}) => {
        this.mailInfo = data.data
        setInterval(() => {
          this.isLoading = false
        }, 300)
      })

    },
    updateMailInfo(req: TEmailInfo, fn: Function) {
      const { onSuccess, } = useRequest(updateMailInfo(req))
      onSuccess((res) => {
        if (res.data) {
          fn && fn()
        }
      })
    }
  },
})

export default useConfigStore
